﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Easy4net.DBUtility;
using JSMS.Model;
using JSMS.Utils;
using Easy4net.Common;
using JSMS.Filter;


namespace JSMS.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        [IsLoginFilter]
        public ActionResult Index()
        {
            //DBHelper db = DBHelper.getInstance();
            string path = Server.MapPath("~//");
            ViewData["p"] = path;
            Session["user"] = new { name="fct",age=20,sex="男"};
            return View();
        }
        public ActionResult FileTree()
        {
            return View();
        }
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(string email,string role,string password)
        {
            DBHelper db = DBHelper.getInstance();
            password = MD5Utlils.GetMd5Hash(password);
            if (role.Equals("student")) {
                string sql = "select * from student where email=@email and password=@password";
                ParamMap param = ParamMap.newMap();
                param.setParameter("email", email);
                param.setParameter("password",password);
                Student student = db.FindOne<Student>(sql, param);
                if(student!=null)
                {
                    Session["role"] = "student";
                    Session["student"] = student;
                    return RedirectToAction("../Student/Index");
                }
                else
                {
                    ViewData["message"] = "登录失败！请重新填写信息!!!";
                    return View("Login");
                }
            }
            else
            {
                string sql = "select * from teacher where email=@email and password=@password";
                ParamMap param = ParamMap.newMap();
                param.setParameter("email", email);
                param.setParameter("password", password);
                Teacher teacher = db.FindOne<Teacher>(sql, param);
                if (teacher != null)
                {
                    Session["role"] = "teacher";
                    Session["teacher"] = teacher;
                    return RedirectToAction("IndexTeacher", "Teacher");//跳转到老师的主页
                }
                else
                {
                    ViewData["message"] = "登录失败！请重新填写信息!!!";
                    return View("Login");
                }
            }
        }

    }
}
